'use strict';

module.exports = app => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;

  const MessageSchema = new Schema({
    /**
     * 消息
     */
    message: { type: String, default: '' },
    /**
     * 昵称
     */
    visitorName: { type: String, default: '' },
    /**
     * Socket Id
     */
    socketId: { type: String, default: '' },
    /**
     * userId
     */
    userId: { type: String, default: '' },
    /**
     * Socket Id
     */
    icon: { type: String, default: '' },
    /**
     * 时间
     */
    date: { type: Date, default: new Date() },
  });

  /**
   * 删除单个数据
   * @param id String 要删除数据的ID
   * @return {*} Promise
   */
  MessageSchema.statics.removeModel = function(id) {
    return this.deleteOne({
      _id: mongoose.Types.ObjectId(id),
    });
  };

  /**
   * 批量删除
   * @param { String[] } ids String 要删除数据的ID数组
   * @return {*} Promise
   */
  MessageSchema.statics.batchDelete = function(ids) {
    return this.deleteMany({ _id: { $in: ids } });
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 获取个数
   */
  MessageSchema.statics.findCount = function() {
    const params = {};
    return this.countDocuments(params);
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 获取列表数据
   */
  MessageSchema.statics.findList = function(data) {
    const params = {};
    const skip = (data.pageNum - 1) * data.pageSize;
    const model = this.find(params).skip(skip).limit(data.pageSize);
    model.sort({ date: -1 });
    return model.exec();
  };

  return mongoose.model('Message', MessageSchema, 'massages');
};
